ExecuteGlobal CreateObject("Scripting.FileSystemObject").OpenTextFile("C1 Invoked Function.vbs",1).ReadAll'调用函数库
'< -----------------End of function-----------------Start Script----------------- >'
GetRequestDetails : winstr = Split(GetC1Env(UserDetail(0)), "cis.jsp", -1, 1)(0) & "uiPage"
FindC1EnvInIE(GetC1Env(UserDetail(0)))
if typename(ie) = "Empty" then
	LogonEnv : SwitchMenu
end if
if trim(UserDetail(2)) <>"" then '<--------------------get info from model ID'
	validUser(UserDetail(2))
	if isErrorIDFound = "true"  then
		msgbox("the model id doesn't exist or has multiple matched value")
		WScript.quit
	end if
	'<-------------------------------------------------Switch to TODO Roles'
	Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
	Set TDList = ie.document.frames(0).document.getelementbyid("tabMenu").contentWindow.document.getelementsbytagname("td")
	SwitchTab("To Do Roles") : GetMoreVlues("userRoleGrid")
	'<----------------------------------------------------------get  model usertodo roles list'
	set fso = createobject("scripting.filesystemobject")
	set file = fso.CreateTextFile(UserDetail(2) & "_" & UserDetail(0) & "_Todo.txt", true)
	REM Set ToDoRoles= LocatePageID("userRoleGrid").document.getelementsbytagname("input")
	Set ToDoRoles= getInputList("userRoleGrid")
	for each item in ToDoRoles
		if item.value = "" then exit for
		ReDim Preserve ModelUserTodoList(ModelUserTodoNum)
		ModelUserTodoList(ModelUserTodoNum) = item.value
		file.WriteLine(ModelUserTodoList(ModelUserTodoNum))
		ModelUserTodoNum =ModelUserTodoNum +1
	Next
	file.Close : set file = nothing : set fso = nothing
	REM set ModelUserTodoList = ModelUserTodoList
else
	'<-------------------------------------------------Switch to TODO Roles'
	Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
	Set TDList = ie.document.frames(0).document.getelementbyid("tabMenu").contentWindow.document.getelementsbytagname("td")
	SwitchTab("To Do Roles")
	WshShell.run "C1Inputs.txt",1,true
	msgbox "Please save TODO roles in the opened C1Inputs.txt file before click OK"
	LoadFile ModelUserTodoList
end if
WshShell.run "C1Inputs.txt",1,true
msgbox "Please save C1 ID in the opened C1Inputs.txt file before click OK"
LoadFile UserIDList 
'<-------------------------------------------------Loop user ID to update Todo Roles
for n = 0  to UBound(UserIDList)
	validUser(UCase(Trim(UserIDList(n))))
	if isErrorIDFound = "false"  then 
		Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
		'<-------------------------------------------------------find if there is more todo roles
		GetMoreVlues("userRoleGrid")
		'<------------------------------------------get  end user todo roles list'
		Set ToDoRoles= getInputList("userRoleGrid")
		dim EndUserTodoListNum
		for each item in ToDoRoles
			if item.value = "" then exit for
			EndUserTodoList = item.value & ";" & EndUserTodoList
			EndUserTodoListNum=EndUserTodoListNum+1
		Next
		set userRoleGrid = LocatePageID("userRoleGrid")
		Set TodoRoleAdd= userRoleGrid.document.getelementbyid("IM_USER_ROLE:0$FirstSec_addBut")
		while TodoRoleAdd Is Nothing
			Wscript.Sleep 200
			Set TodoRoleAdd = userRoleGrid.document.getelementbyid("IM_USER_ROLE:0$FirstSec_addBut")
		wend
		'<------------------------------------------------start to add todo roles'
		if IsEmpty(EndUserTodoList) then '<-----------------------------------------------------------------------end user doesn't have any TODO role previously'
			for num = 1 to  UBound (ModelUserTodoList)
				if num =50 then exit for
				lastnum = num+1 : TodoRoleAdd.click
			next
			WScript.Sleep 1000 
			for num = 0 to  UBound (ModelUserTodoList)
				if num =50 then 
					msgbox ("Please add the last todo roles") : exit for
				end if
				Set TodoInfo= userRoleGrid.document.getelementbyid("USER_ROLE:" & num & "$ROLE_ID")
				TodoInfo.value =ModelUserTodoList(num) : TodoInfo.FireEvent("onblur")
				do while userRoleGrid.document.getelementbyid("USER_ROLE:" & num & "$DESCR").innerHTML="" : WScript.Sleep 100 : Loop
			next
			If lastnum >50  Then msgbox ("Please add the last todo roles")
		else  '<-----------------------------------------------------------------------end user already has TODO role, need to exclude duplication 
			dim lastnum,TodoInfoID : lastnum = EndUserTodoListNum-1
			Set regEx = New RegExp
			for num = 0 to UBound(ModelUserTodoList)
				if lastnum-EndUserTodoListNum-1 >50 then
					msgbox ("Please add the last todo roles")
					exit for
				end if
				str=ModelUserTodoList(num) & ";" : regEx.Pattern = str
				regEx.Global = True : res = regEx.Test(EndUserTodoList) : TodoInfoID = lastnum+1
				if res = "False" then
					set userRoleGrid =  LocatePageID("userRoleGrid")
					Set TodoRoleAdd= userRoleGrid.document.getelementbyid("IM_USER_ROLE:" & lastnum &  "$FirstSec_addBut") 
					while TodoRoleAdd Is Nothing
						Wscript.Sleep 200
						Set TodoRoleAdd = userRoleGrid.document.getelementbyid("IM_USER_ROLE:" & lastnum &  "$FirstSec_addBut") 
					wend
					TodoRoleAdd.click
					Set TodoInfo= userRoleGrid.document.getelementbyid("USER_ROLE:"& TodoInfoID & "$ROLE_ID") 
					while TodoInfo Is Nothing
						WScript.Sleep 100
						Set TodoInfo= userRoleGrid.document.getelementbyid("USER_ROLE:"& TodoInfoID & "$ROLE_ID") 
					wend
					TodoInfo.value = ModelUserTodoList(num) : TodoInfo.FireEvent("onblur")
					do while userRoleGrid.document.getelementbyid("USER_ROLE:"& TodoInfoID &"$DESCR").innerHTML="" : WScript.Sleep 100: Loop
					lastnum=lastnum+1
				end if
			next
		end if
		EndUserTodoList="" : EndUserTodoListNum=0 : lastnum = 0
		ie.document.frames(0).document.getelementbyid("IM_SAVE").click
		WScript.Sleep 3000
	else
		OutputErrorID UserIDList(n)
	end if
next
msgbox "Script finished successfully!"